package crp.services.api.graphql.loaders;

import crp.services.api.graphql.models.ApprovalPayload;
import crp.services.api.graphql.models.UserPayload;
import crp.services.api.graphql.models.WorkflowInstancePayload;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Source;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import java.util.List;

@GraphQLApi
@RequestScoped
public class ApprovalPayloadLoader {
    private final UserFetcher userFetcher;
    private final WorkflowInstanceFetcher workflowInstanceFetcher;

    @Inject
    public ApprovalPayloadLoader(UserFetcher userFetcher,
                                 WorkflowInstanceFetcher workflowInstanceFetcher) {
        this.userFetcher = userFetcher;
        this.workflowInstanceFetcher = workflowInstanceFetcher;
    }

    public List<UserPayload> submittedBy(@Source List<ApprovalPayload> approvals) {
        return userFetcher.fetch(approvals, ApprovalPayload::getSubmittedBy);
    }

    public List<UserPayload> approvedBy(@Source List<ApprovalPayload> approvals) {
        return userFetcher.fetch(approvals, ApprovalPayload::getApprovedBy);
    }

    public List<WorkflowInstancePayload> workflow(@Source List<ApprovalPayload> approvals) {
        return workflowInstanceFetcher.fetch(approvals, ApprovalPayload::getExternalId);
    }
}
